// src/api/api.js
import axios from './axios';

export default {
  // 创建新帖子
  createPost: (post, files) => {
    const formData = new FormData();
    formData.append('title', post.title);
    formData.append('content', post.content);
    formData.append('category', post.category);
    files.forEach(file => {
      formData.append('files', file);
    });

    return axios.post('/posts', formData, { // 请求路径为 /posts
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  },

  // 获取所有帖子
  getAllPosts: () => {
    return axios.get('/posts');
  },

  getPostById: (id) => {
    return axios.get(`/posts/${id}`);
  },

  // 更新帖子
  updatePost: (id, formData) => {
    return axios.put(`/posts/${id}`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  },

  // 删除帖子
  deletePost: (id) => {
    return axios.delete(`/posts/${id}`); // 请求路径为 /posts/{id}
  },

 // 创建评论
 createComment: (postId, commentData) => {
  return axios.post(`/posts/${postId}/comments`, commentData);
},

// 获取评论
getComments: (postId) => {
  console.log('Requesting comments for postId:', postId); 
  return axios.get(`/posts/${postId}/comments`);
},

// 更新评论
updateComment: (commentId, commentData) => {
  return axios.put(`/comments/${commentId}`, commentData);
},

// 获取评论数量
getCommentCount: (postId) => {
  return axios.get(`/posts/${postId}/comments/count`);
},
// 删除评论
deleteComment: (commentId) => {
  return axios.delete(`/comments/${commentId}`);
},

createReply: (commentId, replyData) => {
  return axios.post(`/replies/${commentId}`, replyData);
},

getRepliesByCommentId: (commentId) => {
  return axios.get(`/replies/${commentId}`);
},

likePost(postId) {
  return axios.post(`/posts/${postId}/like`);
},

getMyPosts: () => {
  return axios.get(`/posts/my-posts`);
},

// 获取用户信息
fetchUserProfile: (username) => {
  return axios.get('/personal-center/profile', {
    params: {
      username: username
    }
  });
},
updateUserProfile: (userData) => {
  return axios.post('/personal-center/profile/update', userData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
},

// 添加收藏
addFavorite: (username, postId) => {
  return axios.post('/favorites', {
    username,
    postId
  }, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
},
removeFavorite: (username, postId) => {
  return axios.delete(`/favorites/${postId}`, {
    params: {
      username
    }
  });
},
// 获取收藏列表
getFavorites: (username) => {
  return axios.get('/favorites', {
    params: {
      username
    }
  });
},
};